﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MYTech.Data
{
    /// <summary>
    /// Ini文件操作类
    /// </summary>
    public class IniHelper
    {
        public string Path { get; set; }
        public string Section { get; set; }
        public IniHelper(string path)
        {
            this.Path = path;
        }

        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="iValue">值</param>
        public void WriteString(string key, string iValue)
        {
            WinAPI.WritePrivateProfileString(Section, key, iValue, Path);
        }

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>返回的键值</returns>
        public string GetString(string key, string def)
        {
            StringBuilder temp = new StringBuilder(255);
            WinAPI.GetPrivateProfileString(Section, key, def, temp, 255, Path);
            return temp.ToString();
        }

        public int ReadInt(string key, int def)
        {
            return WinAPI.GetPrivateProfileInt(Section, key, def, Path);
        }

        public void WriteInt(string key, int iVal)
        {
            WriteString(key, iVal.ToString());
        }

        /// <summary>
        /// 删除INI文件中的某个键值
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">要删除的键</param>
        public void DeleteKey(string section, string key)
        {
            WinAPI.WritePrivateProfileString(section, key, null, Path);
        }

        /// <summary>
        /// 删除INI文件中的某个键值
        /// </summary>
        /// <param name="key">要删除的键</param>
        public void DeleteKey(string key)
        {
            DeleteKey(Section, key);
        }

        /// <summary>
        /// 删除INI文件中的某个键值
        /// </summary>
        /// <param name="section">段落</param>
        public void DeleteSection(string section)
        {
            WinAPI.WritePrivateProfileString(Section, null, null, Path);
        }
    }
}
